package com.gfxy.modules.items.controller;

import com.gfxy.modules.items.service.IKyglHxzbService;
import com.gfxy.modules.items.service.IKyglService;
import com.gfxy.modules.items.vo.KyglHxzbVo;
import com.gfxy.modules.items.vo.KyglVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Api(tags="科研管理")
@RestController
@RequestMapping("/items/kyglhxzb")
@Slf4j
public class KyglHxzbController {

    @Autowired
    private IKyglHxzbService kyglService;


    @AutoLog(value = "教学团队-分页列表查询")
    @ApiOperation(value="教学团队-分页列表查询", notes="教学团队-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(@RequestParam(name="date")  String date,
                                   @RequestParam(name="workNo")  String teacherId,
                                   HttpServletRequest req) {
        List<KyglHxzbVo> pagePage = kyglService.selectListPage(date, teacherId);
        return Result.OK(pagePage);

    }
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile file = entity.getValue();// 获取上传文件对象
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {

                return Result.OK(kyglService.importExcel(file));
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.OK("文件导入失败！");
    }
}
